/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.action;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bm.console.business.WebCounselorPhotoService;
import com.bm.console.pojo.WebCounselorPhoto;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;

@Controller
public class WebCounselorPhotoAction {

    @Resource
    WebCounselorPhotoService webCounselorPhotoService;

    @RequestMapping(value = "/webCounselorPhotoList.do")
    public String webCounselorPhotoList(PageBean pageBean, Model model, WebCounselorPhoto webCounselorPhoto) {
        try {
            pageBean = webCounselorPhotoService.queryPageList(webCounselorPhoto, pageBean);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("startRow", pageBean.getStartRow());
            model.addAttribute("entity", webCounselorPhoto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/web/webcounselorphoto/list";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/addWebCounselorPhoto.do", method = RequestMethod.GET)
    public String addWebCounselorPhoto(@ModelAttribute("webCounselorPhoto") WebCounselorPhoto webCounselorPhoto, Model model, String id, String counselorId) {
        Integer prow = 0;
        try {
            // ArrayList<DictCategory> listcate = bbsService.selectByAll();
            if (StringUtils.hasText(id)) {
                WebCounselorPhoto record = webCounselorPhotoService.queryByPrimaryKey(Integer.parseInt(id));
                model.addAttribute("entity", record);
            }
            // model.addAttribute("listcate", listcate);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        model.addAttribute("counselorId", counselorId);
        model.addAttribute("prow", prow);
        return "/web/webcounselorphoto/add";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebCounselorPhoto.do", method = RequestMethod.POST)
    public String saveWebCounselorPhoto(@Validated @ModelAttribute("webCounselorPhoto") WebCounselorPhoto webCounselorPhoto,
                                        BindingResult result, Model model, Integer prow,
                                        HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCounselorPhotoService.saveOrUpdate(webCounselorPhoto);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {

            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);

            }
            e.printStackTrace();
            model.addAttribute("entity", webCounselorPhoto);
            model.addAttribute("ret", ret);
            return "/web/webcounselorphoto/add";
        }
        return "redirect:/webCounselorPhotoList.do?status=";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebCounselorPhotoJson.do", method = RequestMethod.POST)
    @ResponseBody
    public ObjResultException<String> saveWebCounselorPhotoJson(@RequestBody WebCounselorPhoto webCounselorPhoto, Model model,
                                                                HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCounselorPhotoService.saveOrUpdate(webCounselorPhoto);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }

    /**
     * 删除APP
     */
    @RequestMapping("/deleteWebCounselorPhoto.do")
    @ResponseBody
    public ObjResultException<String> deleteWebCounselorPhoto(Integer id) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCounselorPhotoService.delete(id);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 修改状态APP
     */
    @RequestMapping("/updateStatusWebCounselorPhoto.do")
    @ResponseBody
    public ObjResultException<String> updateStatusWebCounselorPhoto(WebCounselorPhoto webCounselorPhoto) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCounselorPhotoService.updateStatus(webCounselorPhoto);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }
}